package com.inspur.oss.filter;

import com.inspur.oss.constants.LoginConstans;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 通过session控制登录权限
 */
public class LoginFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        Object sessionUser = httpServletRequest.getSession().getAttribute(LoginConstans.SESSION_USER);
        if(sessionUser == null){
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/index.jsp");
        }else{
            chain.doFilter(request,response);
        }
    }

    public void destroy() {

    }
}
